From 48ad04a3dd63decb295cd18e8388cf1cd36b2941 Mon Sep 17 00:00:00 2001 From: parkrrrr Date: Fri, 23 Jul 2004 14:22:49 +0000 Subject: [PATCH] consolidate all rad-to-miles calculations, add 'points' option to arc filter --- gpsbabel/arcdist.c | 18 ++++++++++++++---- gpsbabel/grtcirc.c | 16 ++++++++++++++++ gpsbabel/grtcirc.h | 2 ++ gpsbabel/position.c | 4 ++-- 4 files changed, 34 insertions(+), 6 deletions(-) diff --git a/gpsbabel/arcdist.c b/gpsbabel/arcdist.c index 7ee828099..1f5f1d2f1 100644 --- a/gpsbabel/arcdist.c +++ b/gpsbabel/arcdist.c @@ -30,6 +30,7 @@ static double pos_dist; static char *distopt = NULL; static char *arcfileopt = NULL; static char *exclopt = NULL; +static char *ptsopt = NULL; typedef struct { double distance; @@ -42,6 +43,8 @@ arglist_t arcdist_args[] = { {"distance", &distopt, "Maximum distance from arc", ARGTYPE_FLOAT | ARGTYPE_REQUIRED}, {"exclude", &exclopt, "Exclude points close to the arc", ARGTYPE_BOOL}, + {"points", &ptsopt, "Use distance from vertices not lines", + ARGTYPE_BOOL}, {0, 0, 0, 0} }; @@ -78,17 +81,24 @@ arcdist_process(void) if ( argsfound != 2 && strspn(line, " \t\n") < strlen(line)) { warning(MYNAME ": Warning: Arc file contains unusable vertex on line %d.\n", fileline ); } - else if ( lat1 != BADVAL && lon1 != BADVAL && - lat2 != BADVAL && lon2 != BADVAL ) { + else if ( lat2 != BADVAL && lon2 != BADVAL && + (ptsopt || (lat1 != BADVAL && lon1 != BADVAL ))) { QUEUE_FOR_EACH(&waypt_head, elem, tmp) { waypointp = (waypoint *)elem; - dist = linedist(lat1, lon1, lat2, lon2, + if ( ptsopt ) { + dist = gcdist( lat2*M_PI/180.0, lon2*M_PI/180.0, + waypointp->latitude*M_PI/180.0, + waypointp->longitude*M_PI/180.0 ); + } + else { + dist = linedist(lat1, lon1, lat2, lon2, waypointp->latitude, waypointp->longitude ); + } /* convert radians to float point statute miles */ - dist = (((dist * 180.0 * 60.0) / M_PI) * 1.1516); + dist = tomiles(dist); if ( waypointp->extra_data ) { ed = (extra_data *) waypointp->extra_data; diff --git a/gpsbabel/grtcirc.c b/gpsbabel/grtcirc.c index d059e3c84..7a0b01c99 100644 --- a/gpsbabel/grtcirc.c +++ b/gpsbabel/grtcirc.c @@ -34,6 +34,22 @@ static double dotproduct( double x1, double y1, double z1, return (x1*x2+y1*y2+z1*z2); } +/* + * Note: this conversion to miles uses the WGS84 value for the radius of + * the earth at the equator. + * (radius in meters)*(100cm/m) -> (radius in cm) + * (radius in cm) / (2.54 cm/in) -> (radius in in) + * (radius in in) / (12 in/ft) -> (radius in ft) + * (radius in ft) / (5280 ft/mi) -> (radius in mi) + * If the compiler is half-decent, it'll do all the math for us at compile + * time, so why not leave the expression human-readable? + */ + +double tomiles( double rads ) { + const double radmiles = 6378137.0*100.0/2.54/12.0/5280.0; + return (rads*radmiles); +} + double gcdist( double lat1, double lon1, double lat2, double lon2 ) { return acos(sin(lat1)*sin(lat2)+cos(lat1)*cos(lat2)*cos(lon1-lon2)); } diff --git a/gpsbabel/grtcirc.h b/gpsbabel/grtcirc.h index 94c354a08..de84aea4c 100644 --- a/gpsbabel/grtcirc.h +++ b/gpsbabel/grtcirc.h @@ -23,3 +23,5 @@ double gcdist( double lat1, double lon1, double lat2, double lon2 ); double linedist(double lat1, double lon1, double lat2, double lon2, double lat3, double lon3 ); + +double tomiles( double rads ); diff --git a/gpsbabel/position.c b/gpsbabel/position.c index 46bbfc0f4..3aa8b5fef 100644 --- a/gpsbabel/position.c +++ b/gpsbabel/position.c @@ -150,7 +150,7 @@ position_runqueue(queue *q, int nelems, int qtype) comp[i]->longitude); /* convert radians to integer feet */ - dist = (int)((((dist * 180.0 * 60.0) / M_PI) * 1.1516) * 5280.0); + dist = (int)(5280*tomiles(dist)); if (dist <= pos_dist) { switch (qtype) { @@ -273,7 +273,7 @@ radius_process(void) home_pos->longitude); /* convert radians to float point statute miles */ - dist = (((dist * 180.0 * 60.0) / M_PI) * 1.1516); + dist = tomiles(dist); if ((dist >= pos_dist) == (exclopt == NULL)) { waypt_del(waypointp); -- 2.30.2